package com.itheima.controller;
import com.itheima.aoon.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//部门管理控制器
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    EmpService empService;

    @Autowired
    private DeptService deptService;
    /**
     * 查询部门信息
     * @return
     */
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
      log.info("查询所有部门数据");
      List<Dept> deptList=deptService.list();
      return Result.success(deptList);
    }

    @Log
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor=Exception.class)
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门");
        try {
            deptService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }


        empService.deleteByDeptid(id);

        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门: {}",dept);
        deptService.add(dept);
        return Result.success();
    }

    @Log
    @PutMapping
    private Result update(@RequestBody Dept dept){
        log.info("修改部门{}",dept);
        deptService.update();
        return Result.success();
    }











}